package com.gao.mybatis.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Projectname SSM
 * @Filename sqlSeswsion
 * @Author an
 * @Data 2022/7/28 10:35
 * @Description TODO
 */
public class sqlSessionUtil {
    public static SqlSession getSqlSession(){
        SqlSession sqlSession = null;
        try {
            //获取核心配置文件的输入流
            InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
            //获取SqlSessionFactoryBuilder
            SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
            //获取sqlSessionFactory
            SqlSessionFactory build = sqlSessionFactoryBuilder.build(is);
            //获取sqlSession对象
            sqlSession = build.openSession(true);

        } catch (IOException e) {
            e.printStackTrace();
        }

        return sqlSession;
    }
}
